package com.face.base.controller;


import com.face.base.config.Result;
import com.face.base.utils.BaiduAiUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class BaseFaceController {

    /**
     * 人脸检测状态信息。
     */
    private static Boolean isFace;

    /**
     * 基于 百度AI SDK 的人脸识别。
     */
    @Autowired
    private BaiduAiUtil baiduAiUtil;

    /**
     * 人脸检测
     */
    @PostMapping("/faceCheck")
    public Result<String> faceCheck(@RequestParam("userFace") String imgData) {

        // 是资源路径。
        if (imgData.startsWith("data:image/png;base64,")) {
            // 查看
            String image = imgData.substring(22);
            Boolean aBoolean = baiduAiUtil.faceCheck(image);
            System.out.println("人脸对比结果：" + aBoolean);
            if (aBoolean){
                isFace = true;
                return new Result<>(200, String.valueOf(true));
            }
            return new Result<>(200, String.valueOf(false));
        }

        isFace = false;
//        System.out.println("imgData = " + imgData);
        return new Result<>(0, "不支持的格式");
    }

    public static Boolean getIsFace() {
        return isFace;
    }

    public static void setIsFace(Boolean isFace) {
        BaseFaceController.isFace = isFace;
    }
}
